Thread: [Inline assembly] What are exactly clobbers and why they are used?

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    138

    [Inline assembly] What are exactly clobbers and why they are used?

    Hello and happy holidays to everyone! I'm new to GCC inline assembly (and have some vary basic knowledge in general) and I wanted to ask if someone can explain to me what exactly are "clobbers". I tried to read the official docs for extended inline assembly from GCC (Extended Asm (Using the GNU Compiler Collection (GCC))). I will quote the first paragraph:

    While the compiler is aware of changes to entries listed in the output operands, the inline asm code may modify more than just the outputs. For example, calculations may require additional registers, or the processor may overwrite a register as a side effect of a particular assembler instruction. In order to inform the compiler of these changes, list them in the clobber list. Clobber list items are either register names or the special clobbers (listed below). Each clobber list item is a string constant enclosed in double quotes and separated by commas.
    In another sentence, the following is said:

    When the compiler selects which registers to use to represent input and output operands, it does not use any of the clobbered registers. As a result, clobbered registers are available for any use in the assembler code.
    Hmmm. So how the compiler actually works? Isn't inline assembly just pure assembly that gets "inline" literally? So how can the compiler choose to use or not use anything else that I have written in the code? So in the end how I would know which registers will the compiler change and what to use as a clobber?

    Also I heard that some system calls will clobber some registers. Is that true? In that case how would I know which ones will do that? Is there an official documentation that describes system calls in assembly level (at least for Linux) and all that stuff? All I could find is pages talking about system calls in C level from "unistd.h".

    Maybe all that stuff would make sense if I knew assembly better. Also at the time of writing this, I'm going to sleep so I wasn't able to read the whole doc page so there may be more info that make it clear (in that case, a redirect would be appreciated). Tho I wanted to post anyway just to save some time because I suppose most people here are from USA and it's probably day or evening for you there. Thanks a lot in advance!

  2. #2
    Registered User
    Join Date
    Apr 2021
    Posts
    140
    You need to think about where in-line assembly may appear.

    For example, if it appears inside a for loop:

    Code:
        for (int i = 0; i < 10; ++i) {
           __asm__("movl %eax, %edx")
        }
    You need to keep in mind that GCC does not parse the assembly code. So while you look at that and say, "Oh, eax is overwritten by the assembly", GCC looks at it and says, "Ehh, here's a blob of string data that I don't understand. I'll just hand it to the assembler."

    But it's possible that the C-language parts of GCC might say, "Hey, this is a simple count loop from 0 to 10. Let's optimize this and not store the index variable on the stack, but instead we'll just keep it in the AX register!

    Which would be a great idea, except that the inline assembly "clobbers" the AX register. I wonder why the loop doesn't work like expected?

    That is why there is a parseable mechanism for you to tell GCC, "hey, this blob of text will clobber the AX register!" So it can decide, "Ehh, instead of putting our index in AX, let's use BX instead!" That way, you are able to clobber the registers you want, and GCC can store the values it wants to store in safe registers unlikely to get clobbered.

  3. #3
    Registered User
    Join Date
    Oct 2021
    Posts
    138
    Quote Originally Posted by aghast View Post
    You need to think about where in-line assembly may appear.

    For example, if it appears inside a for loop:

    Code:
        for (int i = 0; i < 10; ++i) {
           __asm__("movl %eax, %edx")
        }
    You need to keep in mind that GCC does not parse the assembly code. So while you look at that and say, "Oh, eax is overwritten by the assembly", GCC looks at it and says, "Ehh, here's a blob of string data that I don't understand. I'll just hand it to the assembler."

    But it's possible that the C-language parts of GCC might say, "Hey, this is a simple count loop from 0 to 10. Let's optimize this and not store the index variable on the stack, but instead we'll just keep it in the AX register!

    Which would be a great idea, except that the inline assembly "clobbers" the AX register. I wonder why the loop doesn't work like expected?

    That is why there is a parseable mechanism for you to tell GCC, "hey, this blob of text will clobber the AX register!" So it can decide, "Ehh, instead of putting our index in AX, let's use BX instead!" That way, you are able to clobber the registers you want, and GCC can store the values it wants to store in safe registers unlikely to get clobbered.
    Wow! If I was rating your reply, I would give you 10/10!!! Short explanations but fully detailed!

    So from what you are saying, I see this problem occurring only on loops (and of course in inline function calls that or macros that will get "inlined"/expanded inside a loop) so other than that there is no reason to tell the compiler to clobber these registers right? Especially when you just do system calls and you are done with them after you have used them. Tbh, I don't see who would use inline assembly to do something like storing a value this way when variables exist for that reason. Of course I'm a beginner in assembly so there may be some cases when you will need to use stuff like that to gain performance or even to do stuff that you can't do in C (no "rotate operator" right?) but I personally want to only use them only for doing system calls. Thanks a lot for your help dude!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inline assembly
    By seizmic in forum C++ Programming
    Replies: 8
    Last Post: 09-30-2005, 09:21 AM
  2. Inline assembly
    By ^xor in forum C Programming
    Replies: 13
    Last Post: 07-05-2005, 06:32 AM
  3. inline assembly in dev-cpp
    By variable in forum C Programming
    Replies: 4
    Last Post: 02-16-2005, 10:27 PM
  4. Arrays In Inline x86 Assembly
    By saxman in forum C Programming
    Replies: 17
    Last Post: 07-07-2004, 02:38 PM
  5. Inline Assembly?
    By AlenM in forum C++ Programming
    Replies: 5
    Last Post: 12-07-2001, 12:08 PM

Tags for this Thread